import Vue from 'vue'
import App from './App'
import config from './config'

Vue.config.productionTip = false

App.mpType = 'app'

// 全局配置
Vue.prototype.$config = config;

// 导航守卫
const whiteList = ['/pages/login/login', '/pages/shared/article', '/pages/shared/category']; // 白名单

// 页面跳转拦截器
const navLock = false;
uni.addInterceptor('navigateTo', {
  invoke(e) {
    if (navLock) return false;
    // 检查登录状态
    const userInfo = uni.getStorageSync('userInfo');
    const isWhitePage = whiteList.some(page => e.url.indexOf(page) !== -1);
    
    if (!userInfo && !isWhitePage) {
      console.log('未登录，跳转到登录页');
      uni.redirectTo({ url: '/pages/login/login' });
      return false;
    }
    return true;
  }
});

uni.addInterceptor('switchTab', {
  invoke(e) {
    // 检查登录状态
    const userInfo = uni.getStorageSync('userInfo');
    if (!userInfo) {
      console.log('未登录，跳转到登录页');
      uni.redirectTo({ url: '/pages/login/login' });
      return false;
    }
    return true;
  }
});

const app = new Vue({
  ...App
})
app.$mount()
